void sd_card_fatfs_test(void)
{
    printf("%s\t%d\r\n", __FUNCTION__ , __LINE__);
    res_sd = f_mount(&fs,"0:", 1);
    printf("%s\t%d\r\n", __FUNCTION__ , __LINE__);
    /*----------------------- 格式化测试 -----------------------*/
    /* 如果没有文件系统就格式化创建创建文件系统 */
    if (res_sd == FR_NO_FILESYSTEM) 
    {
        printf(">> SD Card don't have FatFs Type File System!\r\nCreate SD Card FAT32 File System...\r\nPlease Wait some time!\r\n");
        /* 格式化 */
        //res_sd=f_mkfs("0:", 0, NULL, 0);
        MKFS_PARM opt = {0};
        opt.fmt = FM_FAT32; // 选择FAT32格式
        opt.n_fat = 2; // 设定FAT副本数量，通常为2
        opt.align = 1; // 数据区域对齐，默认为1，表示按扇区对齐
        // opt.n_root     不需要设置，因为我们使用的是FAT32，它没有固定大小的根目录
        opt.au_size = 512; // 每簇大小，以字节为单位，这里是假设每簇为1个扇区，即512字节
        
        //res_sd = f_mkfs("0:", &opt, NULL, 0);
        #define FORMAT_WORK_BUF_SZ 4096         // 根据需要调整
        BYTE formatWorkBuf[FORMAT_WORK_BUF_SZ];
        res_sd = f_mkfs("0:", &opt, formatWorkBuf, FORMAT_WORK_BUF_SZ);
        //res_sd = f_mkfs("0:", &opt, NULL, 4096); //根据实际需求调整
        if (res_sd == FR_OK) 
        {
            printf(">> SD Card Create File System success! \r\n");
            /* 格式化后，先取消挂载 */
            res_sd = f_mount(NULL,"0:",0);
            /* 重新挂载 */
            res_sd = f_mount(&fs,"0:",1);
        } else {
//            LED_RED_ON;
            printf(">> Create file system failed! %d \r\n", res_sd);
            while (1);
        }
    } else if (res_sd!=FR_OK) {
        printf(">> SD Card Mount File System failed! %d \r\n",res_sd);
        while (1);
    } else {
        printf(">> Mount file system success! \r\n");
    }
    
    /*----------------------- 文件系统测试：写测试 -----------------------*/
    printf("\r\n---------------FatFs Write Test---------------\r\n");    
    /* 打开文件，如果文件不存在则创建它 */    
    res_sd = f_open(&fnew, "0:FatFs_test.txt",FA_CREATE_ALWAYS | FA_WRITE );
    if ( res_sd == FR_OK )
    {
        printf(">> Open Or Create file success, write data...\r\n");
        /* 将指定存储区内容写入到文件内 */
        res_sd=f_write(&fnew,WriteBuffer,sizeof(WriteBuffer),&fnum);
        if(res_sd==FR_OK)
        {
            printf(">> Write data Success：%d\r\n",fnum);
            printf(">> Write data:\r\n%s\r\n",WriteBuffer);
        }else{
            printf("Write data failed! (%d)\r\n",res_sd);
        }    
        /* 不再读写，关闭文件 */
        f_close(&fnew);
    }else{    
//        LED_RED_ON;
        printf("Open Or Create file failed!\r\n");
    }
    /*----------------------- 文件系统测试：读测试 -----------------------*/
    printf("--------------- Read file test ---------------\r\n");
    res_sd = f_open(&fnew, "0:FatFs_test.txt", FA_OPEN_EXISTING | FA_READ);      
    if(res_sd == FR_OK)
    {
//        LED_BLUE_ON;
        printf(">> Open success...\r\n");
        res_sd = f_read(&fnew, ReadBuffer, sizeof(ReadBuffer), &fnum); 
        if(res_sd==FR_OK)
        {
            printf(">>Read file data success%d\r\n",fnum);
            printf(">>Read data:\r\n%s \r\n", ReadBuffer);    
        }else{
            printf("Read file failed! (%d)\r\n",res_sd);
        }        
    }else{
//        LED_RED_ON;
        printf("Open file failed!\r\n");
    }
    
    /* 不再读写，关闭文件 */
    f_close(&fnew);    
    
    /* 不再使用文件系统，取消挂载文件系统 */
    f_mount(NULL,"0:",1);
    
    while(1)
    {
        
    }
}

while (sd_init())   /* 检测不到SD卡 */
{
    lcd_show_string(30, 130, 200, 16, 16, "SD Card Error!", RED);
    delay_ms(500);
    lcd_show_string(30, 130, 200, 16, 16, "Please Check! ", RED);
    delay_ms(500);
    LED0_TOGGLE();  /* 红灯闪烁 */
}
